package com.chenyuxin.ilp.model.pojo;

import com.chenyuxin.ilp.model.enums.exception.BaseExceptionEnum;
import com.chenyuxin.ilp.model.enums.exception.ExceptionEnums;
import lombok.Getter;

import java.util.LinkedHashMap;
@Getter
public class Result {
    private Integer code;
    private String message;
    private Object data;

    // 私有构造方法
    private Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功静态方法
    public static Result ok(Object data) {
        if(data==null){
            data = new LinkedHashMap<>();
        }
        return new Result(200, "成功", data);
    }
    public static Result ok(String message, Object data) {
        if(data==null){
            data = new LinkedHashMap<>();
        }
        return new Result(200, message, data);
    }
    // 失败静态方法
    public static Result fail(Integer code,String message) {
        return new Result(code, message,new LinkedHashMap<>());
    }
    // 失败静态方法
    public static Result fail(Integer code,String message,Object data) {
        if(data==null){
            data = new LinkedHashMap<>();
        }
        return new Result(code, message,data);
    }
    // 失败静态方法
    public static Result fail(BaseExceptionEnum baseExceptionEnum) {
        return new Result(baseExceptionEnum.getCode(),baseExceptionEnum.getErrorMessage(),new LinkedHashMap<>());
    }
}
